/**

 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:DeptManagerController.java
 * <p>
 * 类产生时间: 2012-5-29 上午12:53:18
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.controller;

import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.constant.ConstantMap;
import com.oceansoft.core.common.util.CommonUtilCache;
import com.oceansoft.core.common.util.GzkExcelUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.*;
import com.oceansoft.core.message.Base64;
import com.oceansoft.core.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理控制器
 * 
 * @author acer
 * 
 */
@Controller
@RequestMapping("/dhyj/exp")
public class ExportManagerController extends BaseController {

	@Autowired
	private ISysDeptService sysDeptService;

	@Resource
	private JdbcTemplate jdbcTemplate;

	@RequestMapping("index")
	public String  dostaj_import() {
	 return "/dhyj/index";
	}

		/**
         * EXCEL导入跟踪人员
         * 先把上传的文件保存为一个临时文件，当数据读取完毕后，删除临时文件
         * @param request
         * @param mfile
         * @return
         */
	@RequestMapping("dodhyj_import")
	public String  dostaj_import(HttpServletRequest request, ModelMap model, HttpServletResponse response, @RequestParam("excelfile") CommonsMultipartFile mfile){
		String imptype=request.getParameter("imptype");
		Message msg = new Message("操作成功!");
			//临时文件的路径
			String tmpFilePath=super.uploadTemp(request, response, mfile);
			List<Dhyj> list=null;
			GzkExcelUtil excelUtil=null;
			try{
				excelUtil=GzkExcelUtil.getInstance();
				String[] array2={"temp1","zjdh","bjdh","sc","kssj","jssj"};
				String[] array1={"temp1","zjdh","bjdh","temp2","temp3","sc","kssj","jssj"};
				String[] array3={"zjdh","bjdh","kssj","jssj","sc"};
				if("1".equals(imptype)){
					list=excelUtil.parseExcelToList(tmpFilePath, array1, Dhyj.class);
				}else if("2".equals(imptype)){
					list=excelUtil.parseExcelToList(tmpFilePath, array2, Dhyj.class);
				}else if("3".equals(imptype)){
					list=excelUtil.parseExcelToList(tmpFilePath, array3, Dhyj.class);
				}
//				list=excelUtil.parseExcelToList(tmpFilePath, array1, Dhyj.class);

				String sql =" insert into ZP_DHYJ_EXP(DBID," +
						"BJDH," +
						"ZJDH," +
						"KSSJ," +
						"JSSJ," +
						"SC  ," +
						"TYPE)values(1,?,?,?,to_date(?,'yyyy-MM-dd HH24:mm:ss'),to_date(?,'yyyy-MM-dd HH24:mm:ss'),?)";

				if(null!=list && !list.isEmpty())
				{
//					//覆盖导入
//					if(StringUtil.isNotNullAndBlank(imptype)&&imptype.equals("0")){
//						jdbcTemplate.execute("truncate table gab_data_yhkzf");
//					}
					for(int i=0;i<list.size();i++)
					{
////						  String date =  DatetimeUtil.dateToStr(DatetimeUtil.strToDate("20"+link.getRwsj()));
////						  if(!date.startsWith("20")&&!date.startsWith("19")){
////							  date =  DatetimeUtil.dateToStr(DatetimeUtil.strToDate("20"+link.getRwsj()));
////						  }
						jdbcTemplate.update(sql,list.get(i).getBjdh(),list.get(i).getZjdh(),list.get(i).getKssj(),list.get(i).getJssj(),list.get(i).getSc(),imptype);
					}
				}
				model.addAttribute("ok", msg);
				model.addAttribute("msg", "操作成功");
			}catch(Exception e){
				e.printStackTrace();
				model.addAttribute("msg", "导入数据异常，请检查文件格式是否与模板相同！");
			}finally{
				if(null!=excelUtil){
					excelUtil.removeInstance();
				}
				//删除临时文件
				super.deleteTemp(tmpFilePath);
			}
		model.addAttribute("imptype", imptype);
		model.addAttribute("type", "1");
		return "/dhyj/index";
	}
}
